﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WLG.Model.Interface;
using WLG.Model.Permissions;

namespace WLG.Controller
{
    public class ApplicationModelController : IApplicationModelController
    {
        IApplicationModel _model;
        IAuthenticationProvider _IAuthenticationProvider;
        INotifyDataProvider _INotifyDataProvider;
        List<IApplicationModelView> _views = new List<IApplicationModelView>();

        public void SetModel(IApplicationModel model)
        {
            _model = model;
            if (_model != null)
                foreach (IApplicationModelView view in _views)
                    _model.AddObserver(view);
        }

        private void SignIn()
        {
            Identity cID = _IAuthenticationProvider.SignIn();
            _model.Notifications = _INotifyDataProvider.GetNotifications(cID);
        }

        public void SetAuthenticationProvider(IAuthenticationProvider authenticationProvider)
        {
            _IAuthenticationProvider = authenticationProvider;
        }

        public void SetNotifyDataProvider(INotifyDataProvider notifyDataProvider)
        {
            _INotifyDataProvider = notifyDataProvider;
        }

        public void AddView(IApplicationModelView view)
        {
            view.SignIn += new SignInDelegate(SignIn);
            _views.Add(view);
            if (_model != null)
                _model.AddObserver(view);
        }
        public void RemoveView(IApplicationModelView view)
        {
            view.SignIn -= new SignInDelegate(SignIn);
            _views.Remove(view);
            if (_model != null)
                _model.RemoveObserver(view);
        }
    }
}
